﻿
//Author: 张亚涛
//createtime:2016-03-10 09:46:35
//Description:

using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using NS.DAL.Systems;
using Model;
using DAL;

namespace DAL
{
    /// <summary>
    /// 该类提供了一系列操作 "Partner" 表的方法;
    /// </summary>
    public class PartnerBase
    {
        const string SQL_Select = "select [ID],[PartnerNo],[PartnerName],[Address],[LianXR],[LianXDH],[Num] from [Partner] ";
        #region .对本表的维护.
        #region select data
        /// <summary>
        /// 返回Partner表的所有数据 
        /// </summary>
        /// <returns></returns>        
        public static List<Partner> GetAllPartner()
        {
            List<Partner> partnerList = new List<Partner>();
            SqlDataReader reader = null;

            string sql = SQL_Select;
            reader = SQLHelper.ExecuteDataReader(sql, null);
            while (reader.Read())
            {
                Partner partner = new Partner(reader);
                partnerList.Add(partner);
            }
            reader.Close();
            return partnerList;
        }
        /// <summary>
        /// 根据Partner表的ID字段返回数据 
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns></returns>       
        public static Partner GetPartnerById(int id)
        {
            Partner partner = null;
            SqlDataReader reader = null;

            string sql = SQL_Select + "where [ID] = @Id";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Id",id)
            };
            reader = SQLHelper.ExecuteDataReader(sql, paras);
            if (reader.Read())
            {
                partner = new Partner(reader);
            }
            reader.Close();
            return partner;
        }
        /// <summary>
        /// 判断推广码是否已存在
        /// </summary>
        /// <param name="partnerNo">推广码</param>
        /// <returns>返回True或Flase;True表示存在，Flase表示不存在</returns>
        public static bool IsExistPartnerNo(string partnerNo)
        {
            string SQL_Select = @"select count(*) from Partner where PartnerNo=@PartnerNo";
            SqlParameter[] parameters ={
                                         new SqlParameter("@PartnerNo",partnerNo)
                                       };
            string ResultStr = SQLHelper.ExecuteScalar(SQL_Select, parameters);
            if (!string.IsNullOrEmpty(ResultStr) && int.Parse(ResultStr) > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 获得赠送体验券数量
        /// </summary>
        /// <param name="partnerNo">推广码</param>
        /// <returns></returns>
        public static int GetNum(string partnerNo)
        {
            int num = 0;
            string SQL_Select = @"select Num from Partner where PartnerNo=@PartnerNo";
            SqlParameter[] parameters ={
                                         new SqlParameter("@PartnerNo",partnerNo)
                                       };
            string ResultStr = SQLHelper.ExecuteScalar(SQL_Select, parameters);
            if (!string.IsNullOrEmpty(ResultStr))
            {
                num = int.Parse(ResultStr);
            }
            return num;
        }

        #endregion
        #region delete data
        /// <summary>
        /// 根据Partner表的ID字段删除数据 
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>        
        public static int DeletePartnerById(int id)
        {
            string sql = "delete from [Partner] where [ID] = @Id";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Id",id)
            };
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        #endregion
        #region update data
        /// <summary>
        /// prepare parameters 
        /// </summary>
        public static SqlParameter[] PrepareCommandParameters(Partner partner)
        {
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Id",partner.Id),
            new SqlParameter("@PartnerNo",partner.PartnerNo),
            new SqlParameter("@PartnerName",partner.PartnerName),
            new SqlParameter("@Address",partner.Address),
            new SqlParameter("@LianXR",partner.LianXR),
            new SqlParameter("@LianXDH",partner.LianXDH),
            new SqlParameter("@Num",partner.Num)    
            };
            return paras;
        }
        /// <summary>
        /// 根据Partner表的ID字段更新数据 
        /// </summary> 
        /// <param name="Partner">partner</param>
        /// <returns></returns>       
        public static int UpdatePartnerById(Partner partner)
        {
            string sql = "update [Partner] set [PartnerNo] = @PartnerNo,[PartnerName] = @PartnerName,[Address] = @Address,[LianXR] = @LianXR,[LianXDH] = @LianXDH,[Num] = @Num where [ID] = @Id";
            SqlParameter[] paras = PrepareCommandParameters(partner);
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        #endregion
        #region insert data
        /// <summary>
        /// 向Partner表插入一条数据，返回自增列数值，插入不成功则返回-1
        /// </summary>
        /// <param name="partner">Partner</param>       
        /// <returns></returns>        
        public static int AddPartner(Partner partner)
        {
            string sql = "insert into [Partner]([PartnerNo],[PartnerName],[Address],[LianXR],[LianXDH],[Num])values(@PartnerNo,@PartnerName,@Address,@LianXR,@LianXDH,@Num)    select @@identity";
            SqlParameter[] paras = PrepareCommandParameters(partner);
            int id = -1;

            string _ID = SQLHelper.ExecuteScalar(sql, paras).ToString();
            int.TryParse(_ID, out id);

            return id;
        }
        #endregion
        #endregion

        #region .SQL语句汇总.
        /// <summary>
        /// 返回Partner表的所有数据 （SQL语句）
        /// </summary>
        /// <returns></returns>        
        public static string GetAllPartner_SQL()
        {
            return SQL_Select;
        }
        /// <summary>
        /// 获得推广用户统计（SQL语句）
        /// </summary>
        /// <param name="partnerNo">推广码</param>
        /// <param name="month">月份</param>
        /// <returns></returns>
        public static string GetStic_SQL(string partnerNo, string month)
        {
            string strSQL = "SELECT " +
                            "	mr.MemberID, " +
                            "	m.TrueName, " +
                            "	m.Mobile, " +
                            "	mr.RelationDate " +
                            "FROM MemberRelation mr " +
                            "LEFT JOIN Member m " +
                            "	ON mr.MemberID = m.OpenID " +
                            "WHERE mr.PartnerID = '" + partnerNo + "' " +
                            "AND CONVERT(varchar(7), mr.RelationDate, 120) = '" + month + "' ";
            return strSQL;
        }
        #endregion
    }
}